package com.dapeis.api.controller;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.Map;
import java.util.Map.Entry;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ag.module.common.api.service.city.ProvinceService;
import com.ag.utils.DictionarySettingUtils;
import com.ag.utils.FileUtils;
import com.dapeis.core.utils.RequirementConfig;
import com.dapeis.core.utils.StylistConfig;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

/**
 * 处理公共数据 与业务无关 Title:ApiCommonController.java ApiCommonController Description:
 * 
 * @author yuanyong
 * @date 2017年4月24日 下午2:46:03 Email: hzyuanyong@126.com
 *
 */
@Controller
@RequestMapping(value = "common")
public class ApiCommonController extends RestController {
    @Resource
    ProvinceService provinceService;

    @RequestMapping("city")
    public @ResponseBody JsonElement syncStaticJs(HttpServletRequest request,
	    HttpServletResponse response) throws IOException {
	//
	// JsonObject jsonObject = new JsonObject();
	// //JsonArray json = provinceService.countrys;
	// //jsonObject.addProperty("succ", "同步成功！");
	// jsonObject.addProperty("datas", provinceService.countrys.toString());
	// return jsonObject;

	String filePath = FileUtils.splicePaths(request.getRealPath(""),
		"resources/js/city_data.js");
	File file = new File(filePath);
	if (!file.exists()) {
	    file.getParentFile().mkdirs();
	    file.createNewFile();
	}

	response.setContentType(MediaType.APPLICATION_JSON_VALUE
		+ ";charset=UTF-8");
	Writer out = new BufferedWriter(new OutputStreamWriter(
		new FileOutputStream(file), "UTF-8"));
	PrintWriter writer = new PrintWriter(out);
	writer.write("COUNTRY_STATIC_JSON = "
		+ provinceService.countrys.toString());
	writer.flush();
	writer.close();

	JsonObject jsonObject = new JsonObject();
	jsonObject.addProperty("succ", "同步成功！");
	return jsonObject;
    }

    @RequestMapping("user_config")
    public @ResponseBody JsonElement userCofig(HttpServletRequest request,
	    HttpServletResponse response) {
	JsonObject jsonObject = buildSuccess();
	Map<String, String> colorMap = DictionarySettingUtils
		.getParameterMap(StylistConfig.LADY_COLOR_OF_SKIN);
	String colorStr ="";
	for(Entry<String, String> entry: colorMap.entrySet()) { 
	    System.out.print(entry.getKey() + ":" + entry.getValue() + "\t"); 
	    colorStr += entry.getKey() + ":'" + entry.getValue() + "'";  
	}
	
	
	Map<String, String> faceMap = DictionarySettingUtils
		.getParameterMap(StylistConfig.LADY_FACE_TYPE_PRE);
	String faceStr ="";
	for(Entry<String, String> entry: faceMap.entrySet()) { 
	    //System.out.print(entry.getKey() + ":" + entry.getValue() + "\t"); 
	    faceStr += entry.getKey() + ":'" + entry.getValue() + "'";  
	}
	
	

	Map<String, String> stylMap = DictionarySettingUtils
		.getParameterMap(StylistConfig.LADY_LOVE_STYLE);
	String styStr ="";
	for(Entry<String, String> entry: stylMap.entrySet()) { 
	    System.out.print(entry.getKey() + ":" + entry.getValue() + "\t"); 
	    styStr += entry.getKey() + ":'" + entry.getValue() + "'";  
	}
	
	
	

	Map<String, String> prolMap = DictionarySettingUtils
		.getParameterMap(StylistConfig.LADY_PROBLEM);
	String proStr ="";
	for(Entry<String, String> entry: prolMap.entrySet()) { 
	   // System.out.print(entry.getKey() + ":" + entry.getValue() + "\t"); 
	    proStr += entry.getKey() + ":'" + entry.getValue() + "'";  
	}
	
	
	Map<String, String> sieMap = DictionarySettingUtils
		.getParameterMap(StylistConfig.LADY_SIZE);
	String sizeStr ="";
	for(Entry<String, String> entry: sieMap.entrySet()) { 
	  //  System.out.print(entry.getKey() + ":" + entry.getValue() + "\t"); 
	    sizeStr += entry.getKey() + ":'" + entry.getValue() + "'";  
	}
	
	
	jsonObject.addProperty("脸型_face", faceStr);
	jsonObject.addProperty("肤色_color", colorStr);
	jsonObject.addProperty("体型_size", sizeStr);
	jsonObject.addProperty("解决问题_problem", proStr);
	jsonObject.addProperty("风格_style", styStr);
	return jsonObject;
    }

    @RequestMapping("requirement_config")
    public @ResponseBody JsonElement requirementConfig(
	    HttpServletRequest request, HttpServletResponse response) {
	
	JsonObject jsonObject = buildSuccess();
	
	Map<String, String> cjMap = DictionarySettingUtils
		.getParameterMap(RequirementConfig.CHANG_JING_PRE);
	String cjStr ="";
	for(Entry<String, String> entry: cjMap.entrySet()) { 
	    cjStr += entry.getKey() + ":'" + entry.getValue() + "'";  
	}
	
	
	Map<String, String> shopMap = DictionarySettingUtils
		.getParameterMap(RequirementConfig.LADY_SHOP_CHOOSE);
	String shopStr ="";
	for(Entry<String, String> entry: shopMap.entrySet()) { 
	    shopStr += entry.getKey() + ":'" + entry.getValue() + "'";  
	}
	
	
	jsonObject.addProperty("场景_scene", cjStr);
	jsonObject.addProperty("商品选择_shop", shopStr);
	
	return jsonObject;
    }
}
